<?php

namespace App\Admin\Controllers;

use App\Models\Scene;
use App\Models\ScenePerson;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class ScenePersonController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(ScenePerson::class, function (Grid $grid) {
            $grid->actions(function ($actions) {
                $actions->disableView();
            });
            $grid->disableExport();
            $grid->filter(function($filter){
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
                // 在这里添加字段过滤器
                $filter->like('name', '姓名');
                $filter->like('department', '部门');
                $filter->like('position', '职责');
                $filter->equal('scene_id', '景区')->select(Scene::pluck('title','id'));
                $filter->equal('sex', '性别')->select( config('options.sex'));

            });
            $grid->id('ID')->sortable();
            $grid->name('姓名');
            $grid->scene_id('景区')->display(function ($val){
                return Scene::find($val)->title;
            });
            $grid->department('部门');
            $grid->position('职责');
            $grid->sex('性别')->display(function ($val){
                return config('options.sex')[$val];
            });
            $grid->phone('联系方式');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(ScenePerson::class, function (Form $form) {
            $form->radio('scene_id', '所属景区')->options(Scene::pluck('title','id'))->rules('required',['required'=>'请填写该项']);
            $form->text('name', '姓名');
            $form->text('department', '部门');
            $form->text('position', '职责');
            $form->radio('sex', '性别')->options(config('options.sex'))->default(0)->rules('required',['required'=>'请填写该项']);
            $form->text('phone','联系方式');
        });
    }
}
